<template>
  <el-dropdown trigger="click" @command="handleCommand">
    <span class="el-dropdown-link">
      <i class="el-icon-more el-icon--right"></i>
    </span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item v-if="menu.type == -1" command="createRequest" icon="el-icon-document-add">新增请求</el-dropdown-item>
      <el-dropdown-item v-if="menu.type == -1" command="createMenu" icon="el-icon-folder-add">新增子目录</el-dropdown-item>
      <el-dropdown-item v-if="menu.type == -1" command="rename" icon="el-icon-edit">重命名</el-dropdown-item>
      <el-dropdown-item command="move" icon="el-icon-rank">移动到</el-dropdown-item>
      <el-dropdown-item command="copy" icon="el-icon-document-copy">复制到</el-dropdown-item>
      <el-dropdown-item command="delete" icon="el-icon-delete">删除</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import { destroy } from '@/api/directory'
import { destroyRequest } from '@/api/request'
import { EventBus } from "@/utils/event-bus";

export default {
  name: "SubMenuButton",
  data() {
    return {};
  },
  props: ['menu' ],
  methods: {
    handleCommand(command){
      let that = this
      let parent = this.menu
      switch(command){
        case 'createRequest':
          that.$store.commit('app/expanded', parent.id)
          that.$store.commit('app/addRequest', parent)
          EventBus.$emit("openRequest", {
            id: '',
            name: 'newRequest',
            parentId: parent.id,
            isNew: true
          });
          break;
        case 'createMenu':
          that.$store.commit('app/expanded', parent.id)
          that.$store.commit('app/addDirectory', parent)
          break;
        case 'rename':
          that.$store.commit('app/updateDirectory', parent)
          break;
        case 'delete':
          that.$confirm('此操作将会删除[' + parent.name + '], 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if(this.menu.type === -1){
              destroy(this.menu.id)
                .then(response => {
                  EventBus.$emit("refresh");
                  this.$message.success('删除成功')
                })
                .catch()
            } else {
              destroyRequest(this.menu.id)
                .then(response => {
                  EventBus.$emit("refresh");
                  this.$message.success('删除成功')
                })
                .catch()
            }
          }).catch();
          break;
      }
    }
  }
};
</script>
